package com.situ.springmvc.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.situ.springmvc.entity.Student;
import com.situ.springmvc.entity.StudentTeacher;
import com.situ.springmvc.entity.Teacher;
import com.situ.springmvc.service.StudentService;
import com.sun.org.apache.regexp.internal.recompile;

@Controller
// 请求映射，怎么样找到这些请求的路径 /student/insert.action
@RequestMapping(value = "/student")
public class StudentController {
	private StudentService studentService = new StudentService();

	@RequestMapping(value = "/insert1")
	public void insert1(String name, Integer age, String gender) {
		System.out.println("StudentController.insert1()");
		System.out.println("name: " + name);
		System.out.println("age: " + age);
		System.out.println("gender: " + gender);
	}
	
	@RequestMapping(value = "/insert2")
	public ModelAndView insert2(Student student) {
		System.out.println("StudentController.insert2()");
		System.out.println(student);
		
		// Model：数据     View：界面
		ModelAndView modelAndView = new ModelAndView();
		// request.setAttribute("student", student);
		modelAndView.addObject("student", student);
		// request.getRequestDispatcher("/student_info.jsp").forward(request,response);
		modelAndView.setViewName("/student_info.jsp");
		return modelAndView;
	}
	
	@RequestMapping(value = "/insert3", method = RequestMethod.POST)
	public String insert3(Student student, Model model) {
		System.out.println("StudentController.insert3()");
		System.out.println(student);
		
		// 放到request域对象中
		model.addAttribute("student", student);
		// 转发到指定页面
		return "/student_info.jsp";
	}
	
	@RequestMapping(value = "/insert")
	public void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("StudentController.insert()");
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		String gender = request.getParameter("gender");
		Student student = null;//new Student(name, Integer.parseInt(age), gender);
		
		HttpSession session = request.getSession();
		
		request.setAttribute("student", student);
		request.getRequestDispatcher("/student_info.jsp").forward(request, response);
	}
	
	@RequestMapping(value = "/deleteAll")
	public String deleteAll(Integer[] ids, HttpServletRequest request) {
		// /deleteAll.action?ids=1&ids=2&ids=3
		String[] idArray = request.getParameterValues("ids");
		
		System.out.println(Arrays.toString(ids));
		System.out.println(Arrays.toString(idArray));
		
		// 重定向到查找所有请求,在SpringMVC中重定向不需要写项目名
		return "redirect:/student/findAll.action";
	}
	
	@RequestMapping("/findAll")
	public String findAll() {
		System.out.println("StudentController.findAll()");
		
		return "/student_list.jsp";
	}
	
	
	@RequestMapping(value = "/insertStudentTeacher1")
	public void insertStudentTeacher1(Student student, Teacher teacher) {
		System.out.println("StudentController.insertStudentTeacher()");
		System.out.println(student);// Student [id=null, name=null, age=null, gender=null]
		System.out.println(teacher);// Teacher [id=null, name=null, age=null]
	}
	
	@RequestMapping(value = "/insertStudentTeacher")
	public void insertStudentTeacher(StudentTeacher studentTeacher) {
		System.out.println("StudentController.insertStudentTeacher()");
		
		System.out.println(studentTeacher);
	}
	
	@RequestMapping(value = "/list")
	@ResponseBody // 加上这个注解之后，返回值会以json格式返回
	public List<Student> list() {
		System.out.println("StudentController.list()");
		List<Student> list = new ArrayList<Student>();
		for (int i = 1; i <= 30; i++) {
			Student student = new Student(i, "张三" + i, 20, "男");
			list.add(student);
		}
		
		return list;
	}
	
	// /student/findById.action?id=100
	@RequestMapping(value = "/findById")
	@ResponseBody
	public Student findById(Integer id) {
		System.out.println("StudentController.findById()");
		System.out.println("id: " + id);
		Student student = new Student(100, "李四", 20, "男");
		return student;
	}
	
	@RequestMapping(value = "/checkName")
	@ResponseBody
	public Map<String, Object> checkName() {
		System.out.println("StudentController.checkName()");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isExist", true);
		return map;
	}
	
	// /student/findByPage.action?pageNo=2&pageSize=5
	// /student/findByPage.action?pageNo=2&pageS=5
	@RequestMapping(value = "/findByPage1")
	public void findByPage1(Integer pageNo,	@RequestParam(value = "pageS", required = true/* ,defaultValue = "5" */) Integer pageSize) {
		System.out.println("StudentController.findByPage()");
		System.out.println("pageNo: " + pageNo);
		System.out.println("pageSize: " + pageSize);
//		if (pageSize == null) {
//			pageSize = 5;
//		}
	}
	
	public void findByPage(@RequestParam(defaultValue = "2")Integer pageNo, @RequestParam(defaultValue = "5")Integer pageSize) {
		System.out.println("StudentController.findByPage()");
		System.out.println("pageNo: " + pageNo);
		System.out.println("pageSize: " + pageSize);
	}
	
	
	
}
